<template>
	<xn-form-container
		:title="formData.userId ? '编辑小程序用户信息' : '增加小程序用户信息'"
		:width="700"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="版本号：" name="version">
				<a-input v-model:value="formData.version" placeholder="请输入版本号" allow-clear />
			</a-form-item>
			<a-form-item label="状态：" name="status">
				<a-input v-model:value="formData.status" placeholder="请输入状态" allow-clear />
			</a-form-item>
			<a-form-item label="openId：" name="openId">
				<a-input v-model:value="formData.openId" placeholder="请输入openId" allow-clear />
			</a-form-item>
			<a-form-item label="IP地址：" name="ip">
				<a-input v-model:value="formData.ip" placeholder="请输入IP地址" allow-clear />
			</a-form-item>
			<a-form-item label="昵称：" name="nickName">
				<a-input v-model:value="formData.nickName" placeholder="请输入昵称" allow-clear />
			</a-form-item>
			<a-form-item label="电话：" name="phone">
				<a-input v-model:value="formData.phone" placeholder="请输入电话" allow-clear />
			</a-form-item>
			<a-form-item label="头像：" name="headImg">
				<a-input v-model:value="formData.headImg" placeholder="请输入头像" allow-clear />
			</a-form-item>
			<a-form-item label="省：" name="province">
				<a-input v-model:value="formData.province" placeholder="请输入省" allow-clear />
			</a-form-item>
			<a-form-item label="市：" name="city">
				<a-input v-model:value="formData.city" placeholder="请输入市" allow-clear />
			</a-form-item>
			<a-form-item label="区：" name="district">
				<a-input v-model:value="formData.district" placeholder="请输入区" allow-clear />
			</a-form-item>
			<a-form-item label="地址：" name="address">
				<a-input v-model:value="formData.address" placeholder="请输入地址" allow-clear />
			</a-form-item>
			<a-form-item label="姓名：" name="userName">
				<a-input v-model:value="formData.userName" placeholder="请输入姓名" allow-clear />
			</a-form-item>
			<a-form-item label="性别：" name="gender">
				<a-input v-model:value="formData.gender" placeholder="请输入性别" allow-clear />
			</a-form-item>
			<a-form-item label="组织机构ID：" name="orgId">
				<a-input v-model:value="formData.orgId" placeholder="请输入组织机构ID" allow-clear />
			</a-form-item>
			<a-form-item label="组织机构名称：" name="orgName">
				<a-input v-model:value="formData.orgName" placeholder="请输入组织机构名称" allow-clear />
			</a-form-item>
			<a-form-item label="岗位名称：" name="postName">
				<a-input v-model:value="formData.postName" placeholder="请输入岗位名称" allow-clear />
			</a-form-item>
			<a-form-item label="是否内部账号 0=否1=是：" name="innerAccount">
				<a-input v-model:value="formData.innerAccount" placeholder="请输入是否内部账号 0=否1=是" allow-clear />
			</a-form-item>
			<a-form-item label="平台用户ID：" name="platformUserId">
				<a-input v-model:value="formData.platformUserId" placeholder="请输入平台用户ID" allow-clear />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="appletUserForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import appletUserApi from '@/api/biz/hidden/appletUserApi'
	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
	}
	// 默认要校验的
	const formRules = {
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			appletUserApi
				.appletUserSubmitForm(formDataParam, formDataParam.userId)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
